package com.yuyou.fn.common.rocketmq;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

public class Producer {

    private String namesrvAddr ;

    private DefaultMQProducer producer ;

    public String getNamesrvAddr() {
        return namesrvAddr;
    }

    public void setNamesrvAddr(String namesrvAddr) {
        this.namesrvAddr = namesrvAddr;
    }

    public void start(){
        producer = new DefaultMQProducer("yyfn");
        producer.setNamesrvAddr(namesrvAddr);
        producer.setRetryTimesWhenSendFailed(10);
        producer.setRetryTimesWhenSendAsyncFailed(10);
        try {
            producer.start();
        } catch (MQClientException e) {
            throw new RuntimeException("Start mq fail",e);
        }
    }

    public void shutdown(){
        producer.shutdown();
    }

    public void send(String topic, String tag , byte[] bytes) {
        Message message = new Message(topic,tag,bytes);
        try {
            producer.send(message);
        } catch (Exception e) {
            throw new RuntimeException("Send mq fail",e );
        }
    }

    public void send(String topic, String tag , byte[] bytes ,int delayTimeLevel) {
        Message message = new Message(topic,tag,bytes);
        message.setDelayTimeLevel(delayTimeLevel);
        try {
            producer.send(message);
        } catch (Exception e) {
            throw new RuntimeException("Send mq fail",e );
        }
    }
}
